<?php
// 商家
//Load('extend');
class BusinessesAction extends Action {
    
	//商品分类页
	public function index(){
		
		
		if(isset($_GET['aid'])){
			$aid = intval($_GET['aid']);
			$_SESSION['aid'] = $aid;
		}else{
			if(isset($_SESSION['aid'])) $aid = $_SESSION['aid'];
			else $aid=0;
		}

		//获取站点名称
		if(!empty($_SESSION['area'])){
			$area = $_SESSION['area'];
		}else{
			$area = getAreaName($aid);
		}

		//获取顶部广告
		$m = M('advert');	  
		$map=array('status'=>1);
		//if($aid) $map['aid']=$aid;
		$advlist = $m->where($map)->limit(10)->select();

		$catid = intval($_GET['catid']);
		$catename = getCategoryName($catid);

		import('ORG.Util.Page');// 导入分页类
		$m = M('Businesses');
		$map = array('status'=>1);
		//$map['recommend']=0;
		if($aid) $map['aid']=$aid;
		if($catid) $map['catid']=$catid;
		$kw = isset($_GET['kw']) && !empty($_GET['kw']) ? addslashes($_GET['kw']) : false;
		if($kw){
			$map['name'] = array('like','%'.$_GET['kw'].'%');
			if(isset($map['catid'])) unset($map['catid']);
		}

		$count = $m->where($map)->count();// 查询满足要求的总记录数
		$Page = new Page($count,16);// 实例化分页类 传入总记录数
		$Page->rollPage = 5;	//最多显示20个分页
		
		// 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
		$nowPage = isset($_GET['p'])? intval($_GET['p']):1;
		
		//普通商家列表
		$list = $m->where($map)->order('price desc, create_time desc')->page($nowPage.','.$Page->listRows)->select();
		$Page->setConfig('theme','%upPage% %downPage% %prePage% %linkPage% %nextPage%');
		$page = trim( $Page->show() );// 分页显示输出

		
		//推荐商家列表
		$map['recommend']=1;
		$r_list = $m->where($map)->order('price desc, create_time desc')->limit(8)->select();
		//echo $m->getLastSql();

		//获取站点名称
		if(!empty($_SESSION['webname']))
			$webname = $_SESSION['webname'];
		else 
			$webname = getWebname($aid);
		
		$this->assign('aid',$aid);
		$this->assign('area',$area);
		$this->assign('webname',$webname);
		$this->assign('advlist',$advlist);
		$this->assign('catid',$catid);
		$this->assign('catename',$catename);
		$this->assign('kw',$kw);
		$this->assign('list',$list);
		$this->assign('page',$page);
		$this->assign('r_list',$r_list);
		$this->display();
    }
	
	
	//显示详细
	public function show()
	{
		if(isset($_GET['aid'])){
			$aid = intval($_GET['aid']);
			$_SESSION['aid'] = $aid;
		}else{
			if(isset($_SESSION['aid'])) $aid = $_SESSION['aid'];
			else $aid=0;
		}

		//获取站点名称
		if(!empty($_SESSION['area'])){
			$area = $_SESSION['area'];
		}else{
			$area = getAreaName($aid);
		}

		$id = $this->_get('id');
		$m = M('Businesses');
		$m->where(array('id'=>$id))->setInc('num',1);
		$Businesses = $m->find($id);

		$Businesses['create_time'] = date('Y-m-d',strtotime($Businesses['create_time']));
		$Businesses['pics'] = json_decode($Businesses['pics'],1);
		//$Businesses['intro'] = htmlspecialchars($Businesses['intro']);

		$this->assign('aid',$aid);
		$this->assign('area',$area);
		$this->assign('Businesses',$Businesses);
		$this->display();
	}

	public function order()
	{

		$m = M('Businesses');
		$re = $m->select();
		foreach($re as $val){
			if( !empty($val['pics']) ){
				$data = json_encode(array($val['pics']));
				$sql = 'update home_businesses set pics=\''.$data.'\' where id='.$val['id'];
				$m->query($sql);
				echo $m->getLastSql(),'; <>';
			}
		}
		
	}
	

}

